import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login.vue'
import regist from '@/views/regist.vue'
import Index from '@/views/index.vue'
import Home from '@/views/Home.vue'
import SysUser from '@/views/SysUser.vue'
import AppUser from '@/views/AppUser.vue'
import UserAdd from '@/views/UserAdd.vue'
import axios from 'axios'
import Shows from '@/views/Shows.vue'
import Tags from '@/views/Tags.vue'
import Music from '@/views/Music.vue'
import Area from '@/views/Area.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/index'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/regist',
    name: 'regist',
    component: regist
  },
  {
    path: '/index',
    component: Index,
    children: [
      { path: '/home', component: Home },
      { path: '/sysuser', component: SysUser },
      { path: '/appuser', component: AppUser },
      { path: '/useradd', component: UserAdd },
      { path: '/area', component: Area },
      { path: '/tags', component: Tags },
      { path: '/Shows', component: Shows },
      { path: '/music', component: Music }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  if (!localStorage.getItem('username')) return next('/login')
  axios.defaults.headers.common.Authorization = sessionStorage.getItem('token')
  next()
})

export default router
